package com.jw.blog.wblog.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.blog.annotation.SystemLog;
import com.jw.blog.utils.ConstantUtil;
import com.jw.blog.wblog.entity.Resource;
import com.jw.blog.wblog.entity.custom.ResultBean;
import com.jw.blog.wblog.service.IResourceService;
import lombok.Data;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author wan Email-wlf18726994755@163.com
 * @version 1.0
 * @className ResourceControllerAdmin
 * @date 2020/6/21 15:08
 **/

@RestController
public class ResourceControllerAdmin {

    /**
     * 内部类作为返回数据的格式
     */
    @Data
    static
    class StatuCount implements Serializable {
        private static final long serialVersionUID = 1L;
        private Integer status;
        private Integer count;
    }


    @Autowired
    private IResourceService iResourceService;

    @GetMapping("/admin/selectResourceListByStatus")
    public ResultBean<List<StatuCount>> selectResourceListByStatus() {

        QueryWrapper<Resource> blogQueryWrapper;

        ArrayList<StatuCount> statuCounts = new ArrayList<>();
        for (int i = -1; i < 2; i++) {
            blogQueryWrapper = new QueryWrapper<>();
            if (i == 0) {
                continue;
            }
            blogQueryWrapper.lambda().eq(Resource::getStatus, i).select(Resource::getStatus);
            List<Resource> list = iResourceService.list(blogQueryWrapper);
            StatuCount statuCount = new StatuCount();
            statuCount.setStatus(i);
            statuCount.setCount(list.size());
            statuCounts.add(statuCount);
        }
        return new ResultBean<>(statuCounts);

    }

    @PostMapping("/admin/selectResourceById")
    public ResultBean<Resource> selectResourceById(@RequestParam(value = "id") Integer id) {
        return new ResultBean<>(iResourceService.getById(id));
    }
    @PostMapping("/admin/updateResource")
    @SystemLog(description = ConstantUtil.RESOURCE_UPDATE,userType=ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> updateResource(String prarm,Resource resource) {
        ResultBean<Object> resultBean = new ResultBean<>();
        if (iResourceService.updateById(resource)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            // 0表示：更新失败
            resultBean.setCode(ResultBean.NO_PERMISSION);
        }
        return resultBean;
    }

    @PostMapping("/admin/deleteResource")
    @SystemLog(description = ConstantUtil.RESOURCE_DELETE,userType=ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> deleteResource(String prarm,@RequestParam(value = "id") Integer id) {
        ResultBean<Object> resultBean = new ResultBean<>();
        if (iResourceService.removeById(id)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            // 0表示：删除失败
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }


    @PostMapping("/admin/addResource")
    @SystemLog(description = ConstantUtil.RESOURCE_ADD,userType= ConstantUtil.USERTYPE_ADMIN)
    public ResultBean<Object> addResource(String prarm,Resource resource) {
        ResultBean<Object> resultBean = new ResultBean<>();
        resource.setAddTime(LocalDateTime.now());
        if (iResourceService.save(resource)) {
            resultBean.setCode(ResultBean.SUCCESS);
        } else {
            // 0表示：插入失败
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }


    @PostMapping("/admin/selectLikeResourceListByPage")
    public ResultBean<IPage<Resource>> selectLikeResourceListByPage(
            String prarm, Resource resource,
            @RequestParam(value = "page", required = true, defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", required = true, defaultValue = "9") Integer pageSize
    ) {
        QueryWrapper<Resource> resourceQueryWrapper = new QueryWrapper<>();
        if (!StrUtil.hasEmpty(resource.getTitle())) {
            resourceQueryWrapper.lambda().like(Resource::getTitle, resource.getTitle());
        }

        if (!StrUtil.hasEmpty(resource.getContent())) {
            resourceQueryWrapper.lambda().like(Resource::getContent, resource.getContent());
        }
        if (resource.getStatus() != 0) {
            resourceQueryWrapper.lambda().eq(Resource::getStatus, resource.getStatus());
        }
        resourceQueryWrapper.lambda().orderByDesc(Resource::getAddTime);
        Page<Resource> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(pageSize);
        return new ResultBean<>(iResourceService.page(page1, resourceQueryWrapper));
    }
}
